package cn.sxt.yxl.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.yxl.common.util.PageInfoUtil;
import cn.sxt.yxl.users.dao.IAAdminsDao;
import cn.sxt.yxl.users.dao.IARoleDao;
import cn.sxt.yxl.users.pojo.AAdmins;
import cn.sxt.yxl.users.pojo.ARole;
import cn.sxt.yxl.users.service.IUsersService;

@Service("usersService")
public class UsersServiceImpl implements IUsersService{

	@Resource
	private IAAdminsDao adminsDao;
	@Resource
	private IARoleDao roleDao;
	/**
	 * 保存一条管理员
	 */
	@Override
	public JSONObject saveOneAdminsService(AAdmins admins) {
		JSONObject resultJson = new JSONObject();
		int res = adminsDao.saveOne(admins);
		if(res > 0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admins.getId());
			dataJson.put("effectCount", res);
			resultJson.put("data", dataJson);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	/**
	 * 更新一条管理员
	 */
	@Override
	public JSONObject updateOneAdminsService(AAdmins admins) {
		JSONObject resultJson = new JSONObject();
		int res = adminsDao.updateOne(admins);
		if(res > 0 ){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admins.getId());
			dataJson.put("effectCount", res);
			resultJson.put("data", dataJson);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	/**
	 * 删除一条管理员
	 */
	@Override
	public JSONObject deleteOneAdminsService(AAdmins admins) {
		JSONObject resultJson = new JSONObject();
		int res = adminsDao.deleteOne(admins);
		if(res > 0 ){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", admins.getId());
			dataJson.put("effectCount", res);
			resultJson.put("data", dataJson);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	/**
	 * 查询一条管理员
	 */
	@Override
	public AAdmins findOneAdminsService(Map<String, Object> condMap) {
		return adminsDao.findOne(condMap);
	}

	/**
	 * 查询多条管理员（分页/不分页）
	 */
	@Override
	public List<AAdmins> findCondListAdminsService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			//获取总记录数前需要先查询pagehelper才会生效，有值
			List<AAdmins> adminsList = this.adminsDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.parseInt(page.getTotal()+""));
			return adminsList;
		}
		return adminsDao.findCondList(condMap);
	}

	/**
	 * 保存一条角色
	 */
	@Override
	public JSONObject saveOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int res = roleDao.saveOne(role);
		if(res > 0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId());
			dataJson.put("effectCount", res);
			resultJson.put("data", dataJson);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	/**
	 * 更新一条角色
	 */
	@Override
	public JSONObject updateOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int res = roleDao.updateOne(role);
		if(res > 0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId());
			dataJson.put("effectCount", res);
			resultJson.put("data", dataJson);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	/**
	 * 删除一条角色
	 */
	@Override
	public JSONObject deleteOneRoleService(ARole role) {
		JSONObject resultJson = new JSONObject();
		int res = roleDao.deleteOne(role);
		if(res > 0){
			JSONObject dataJson = new JSONObject();
			dataJson.put("id", role.getId());
			dataJson.put("effectCount", res);
			resultJson.put("data", dataJson);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	/**
	 * 查询一条角色
	 */
	@Override
	public ARole findOneRoleService(Map<String, Object> condMap) {
		return this.roleDao.findOne(condMap);
	}

	/**
	 * 查询多条角色（分页/不分页）
	 */
	@Override
	public List<ARole> findCondListRoleService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
		if(pageInfoUtil != null){
			Page page = PageHelper.startPage(pageInfoUtil.getCurrentPage(), pageInfoUtil.getPageSize());
			List<ARole> roleList = this.roleDao.findCondList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return roleList;
		}
		return this.roleDao.findCondList(condMap);
	}

}
